package net.oschina.app.viewpagerfragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.oschina.app.R;
import net.oschina.app.base.BaseFragment;
import net.oschina.app.fragment.DefaultFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*1. 滑动界面实现步骤
        * 1. 初始化布局, 将包含页面滑动标签(PageSlidngTabStrip\TabLayout),ViewPager的布局初始化
        LayoutInflate
        （TabLayout\PageSlidngTabStrip)+ViewPager+Fragment
        * 2. 查找关心的控件
        TabLayout
        ViewPager
        * 3. 创建数据适配器   PagerAdapter
        FragmentPagerAdapter--> FragmentStatePagerAdapter
        * 4. 初始化页签信息
        : 标签标题   标签所对应的Fragment   传递给Fragment的参数
        * 5. 将数据适配器设置给ViewPager
        * 6. 页签布局(TabLayout)要和ViewPager要绑定/关联*/
public class NewsVPFragment_1 extends BaseFragment {
    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.vp_pager)
    ViewPager mViewPager;
    private Context context;

    //1.初始化片段
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getContext();
    }

    //2. 初始化片段的界面，返回的是片段的根节点视图
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

       /*************************与界面UI相关的操作 ************************/
        //1. 初始化布局, 将包含页面滑动标签(PageSlidngTabStrip\TabLayout),ViewPager的布局初始化
        View view = View.inflate(context, R.layout.fragment_news_vp, null);
        //2. 绑定控件，查找关心的控件
        ButterKnife.bind(this, view);
        //3. 创建数据适配器   PagerAdapter
          NewsVPAdapter mAdapter =new NewsVPAdapter(getChildFragmentManager());

        //4. 将数据适配器设置给ViewPager
        mViewPager.setAdapter(mAdapter);

        //5. 页签布局(TabLayout)要和ViewPager要绑定/关联
        mTabLayout.setupWithViewPager(mViewPager);



        /*************************与数据相关的操作 ************************/
        //6. 初始化页签信息,包含（标签标题   标签所对应的Fragment   传递给Fragment的参数）
        //6.1 向适配器添加片段
        DefaultFragment defaultFragment=new DefaultFragment();
        Bundle bundle=getBundle("资讯内容");
        defaultFragment.setArguments(bundle);
        mAdapter.addFragment(defaultFragment);


         defaultFragment=new DefaultFragment();
        defaultFragment.setArguments(getBundle("热点内容"));
        mAdapter.addFragment(defaultFragment);


        defaultFragment=new DefaultFragment();
        mAdapter.addFragment(defaultFragment);


        defaultFragment=new DefaultFragment();
        mAdapter.addFragment(defaultFragment);

        return view;
    }


    //取得bundle对象
    private Bundle getBundle(String info) {
        Bundle bundle=new Bundle();
        bundle.putString("key",info);
        return  bundle;
    }

    //创建一个片段的页面适配器,继承FragmentStatePagerAdapter
    public class NewsVPAdapter extends FragmentStatePagerAdapter {
        private List<BaseFragment> fragments=new ArrayList<>();
        //对外提供一个添加片段的方法
        public void addFragment(BaseFragment fragment){
            fragments.add(fragment);
            notifyDataSetChanged();//发一个数据改变的通知
        }

        public NewsVPAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        //取得要装配的页面的数量
        @Override
        public int getCount() {
            return fragments.size();
        }
        //取得页面标题， Tablayout的页签标题要依赖该方法
        @Override
        public CharSequence getPageTitle(int position) {
            return getResources().getStringArray(R.array.news_viewpage_arrays)[position];
        }
    }
}
